package com.contract.management.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.contract.management.domain.CommJpaRepository;
import com.contract.management.service.ContractService;
import com.contract.management.util.StrUtils;

@Controller
@RequestMapping("/statis")
public class StatisController {
	
	@Value("${office.basepath}")
	private String basePath;
	
	@Autowired
	private CommJpaRepository repo;

	@Autowired
	private ContractService service;
	
	@RequestMapping("/flow")
	public String flow(Model model) {
		model.addAttribute("ysq", repo.queryOne("select count(*) from Contract c where c.curNode.id <= ?0", 8));
		model.addAttribute("spz", repo.queryOne("select count(*) from Contract c where c.curNode.id <= ?0", 7));
		model.addAttribute("clz", repo.queryOne("select count(*) from Contract c where c.curNode.id > ?0 and c.curNode.id < ?1", 8, 13));
		model.addAttribute("czbj", repo.queryOne("select count(*) from Contract c where c.curNode.id = ?0", 13));
		return "statis_flow";
	}
	
	@RequestMapping("/flow/list")
	@ResponseBody
	public List<Object[]> flowList(Integer status, String no, String jfWtr) {
		if(status == null) status = 1;
		StringBuilder sql = new StringBuilder("select c.id, c.no, c.area, c.company.name, c.contSmallClass.name, c.ycfq, c.isHjb, c.jfWtr, c.jfLxr, c.fyhj, c.nodeInfo, c.user.name, c.createTime, c.nextOper, c.curNode.id, c.type from Contract c where");
		Map<String, Object> params = new HashMap<>();
		switch (status) {
		case 1:	//已申请合同：已经生成合同号，并且盖章但未上传合同资料的合同，
			sql.append(" c.curNode.id <= :nodeId");
			params.put("nodeId", 8);
			break;
		case 2:	//审批中合同：已经生成合同号，但是未盖章
			sql.append(" c.curNode.id <= :nodeId");
			params.put("nodeId", 7);
			break;
		case 3:	//进度处理中合同：上传资料后，各部门报件前
			sql.append(" c.curNode.id > :snodeId and c.curNode.id < :bnodeId");
			params.put("snodeId", 8);
			params.put("bnodeId", 13);
			break;
		case 4:	//操作报件合同：各部门报件之后
			sql.append(" c.curNode.id = :nodeId");
			params.put("nodeId", 13);
			break;
		default:
			break;
		}
		if(!StrUtils.isEmpty(no)) {
			sql.append(" and c.no = :no");
			params.put("no", no);
		}
		if(!StrUtils.isEmpty(jfWtr)) {
			sql.append(" and c.jfWtr like :jfWtr");
			params.put("jfWtr", "%" + jfWtr.trim() + "%");
		}
		return repo.query(sql.toString(), params);
	}
	
	@RequestMapping("/ywlx")
	public String ywlx(Model model) {
		model.addAttribute("ywlxs", repo.query("select new map(s.id as id, concat((CASE s.bigClass.area WHEN 'A' THEN '上海' WHEN 'B' THEN '苏州' WHEN 'C' THEN '成都' END), '-',  s.bigClass.name, '-', s.name) as name) from ContSmallClass s order by s.id"));
		return "statis_ywlx";
	}
	
	@RequestMapping("/ywlx/list")
	@ResponseBody
	public List<Object[]> ywlxList(int ywlx) {
		StringBuilder sql = new StringBuilder("select c.id, c.no, c.area, c.company.name, c.contSmallClass.name, c.ycfq, c.isHjb, c.jfWtr, c.jfLxr, c.fyhj, c.nodeInfo, c.user.name, c.createTime, c.nextOper, c.curNode.id, c.type from Contract c ");
		Map<String, Object> params = new HashMap<>();
//		sql.append(" c.curNode.id = :nodeId");
//		params.put("nodeId", 13);
		
		if(ywlx != 0) {
			sql.append("where c.contSmallClass.id = :ywlxId");
			params.put("ywlxId", ywlx);
		}
		return repo.query(sql.toString(), params);
	}

}
